package com.hankol.newretail.viewmodel

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.baselibrary.base.BasicListBean
import com.baselibrary.base.mvvm.BaseViewModel
import com.baselibrary.extension.checkNotNull
import com.baselibrary.utils.AmountUtil
import com.baselibrary.utils.SpUtils
import com.hankol.newretail.adapter.cart.ShopCartGroupAdapter
import com.hankol.newretail.bean.AfterSalesRevokeBean
import com.hankol.newretail.bean.GoodListBean
import com.hankol.newretail.bean.ShopCartAddBean
import com.hankol.newretail.bean.ShopCartBean
import com.hankol.newretail.respository.NewReatilRequestManager

/**
 * @author: 关忆北、
 * @date: 2022/8/5
 * @Des：
 */
class ShopCartViewModel : BaseViewModel() {

    var shopCartLD: MutableLiveData<ArrayList<ShopCartBean>> = MutableLiveData()
    var selectCartShopLD: MutableLiveData<Boolean> = MutableLiveData()
    var delCartShopLD: MutableLiveData<AfterSalesRevokeBean> = MutableLiveData()
    var addCartShopLD: MutableLiveData<ShopCartAddBean> = MutableLiveData()

    //猜你喜欢 商品  分页列表
    var guessYouLikeLD: MutableLiveData<BasicListBean<GoodListBean>> = MutableLiveData()
    fun reqShopCartList() {
        NewReatilRequestManager.getInstance().reqShopCartList(shopCartLD)
    }

    fun reqSelectCartShop(cartIds: String, selected: Int) {
        NewReatilRequestManager.getInstance().reqSelectCartShop(cartIds, selected, selectCartShopLD)
    }

    fun reqDelCartShop(ids: String) {
        NewReatilRequestManager.getInstance().reqDelCartShop(ids, delCartShopLD)
    }

    fun reqAddCartShop(skuid: Long, num: Int) {
        NewReatilRequestManager.getInstance().reqAddCartShop(0, skuid, num, addCartShopLD)
    }

    fun changePrice(adapter: ShopCartGroupAdapter): Long {
        var sumPrice: Long = 0
        val dataList = adapter.data
        for (data in dataList) {
            for (item in data.mallCartSkuDtoList) {
                if (item.status == 0 && item.selected == 1) {
                    if (item.seckillActProductDTO.checkNotNull()) {
                        var startTime = item.seckillActProductDTO.stime
                        var endTime = item.seckillActProductDTO.etime
                        if (System.currentTimeMillis() < startTime) {
                            sumPrice += (item.price * item.num)
                        } else if (System.currentTimeMillis() in startTime..endTime) {
                            var seckillPrice = if (SpUtils.getInstance().isFans) item.seckillActProductDTO.fansPrice else item.seckillActProductDTO.price
                            sumPrice += (seckillPrice * item.num)
                        }

                    } else {
                        sumPrice += (item.price * item.num)
                    }
                }
            }
        }
        return sumPrice
    }

    fun changeGoodsPiece(adapter: ShopCartGroupAdapter): Long {
        var sumPrice: Long = 0
        adapter.data.forEach { group ->
            group.mallCartSkuDtoList.forEach { child ->
                if (child.status == 0 && child.selected == 1) {
                    sumPrice += child.num
                }
            }
        }
        return sumPrice
    }

    //存储选中的商品的酒窖的id 用于判断是否选择了多个酒窖
    var shopIdList = mutableSetOf<Long>()
    fun getSelectCartIds(adapter: ShopCartGroupAdapter): String {
        if (adapter.data.size == 0) return ""
        shopIdList.clear()
        val buffer = StringBuffer();
        buffer.append("[")
        adapter.data.forEach { group ->
            group.mallCartSkuDtoList.forEach { child ->
                if (child.status == 0 && child.selected == 1) {
                    buffer.append(child.cartId).append(",")
                    shopIdList.add(child.shopId)
                }
            }
        }
        buffer.deleteCharAt(buffer.length - 1)
        buffer.append("]")
        return buffer.toString()
    }

    fun setSelectAllCartIds(adapter: ShopCartGroupAdapter, position: Int, isSelected: Boolean): String {
        val buffer = StringBuffer();
        buffer.append("[")
        adapter.data[position].mallCartSkuDtoList.forEach { child ->
            if (child.status == 0) {
                child.selected = if (isSelected) 1 else 0
                buffer.append(child.cartId).append(",")
            }
        }
        if (buffer.length > 2) {
            buffer.deleteCharAt(buffer.length - 1)
        }
        buffer.append("]")
        return buffer.toString()
    }

    /**
     * 判断购物车是否所有店铺都空数据
     */
    fun isCartEmpty(cartGroupAdapter: ShopCartGroupAdapter): Boolean {
        cartGroupAdapter.childAdapterList.forEach {
            if (it.data.isNotEmpty()) {
                return false
            }
        }
        return true
    }

    /**
     *猜你喜欢 商品  分页列表
     */
    fun reqGuessYouLikeGoodList(page: Int) {
        NewReatilRequestManager.getInstance().reqRecommendGoods(guessYouLikeLD, "", 3, 0, page, 20)
    }
}